package me.ywy.apt.ormdb;

import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.List;

import me.ywy.apt.ormdb.sqlite.BaseDaoFactory;
import me.ywy.apt.ormdb.sqlite.IBaseDao;
import me.ywy.apt.ormdb.utils.BitmapUtils;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button insertDb;
    private IBaseDao<Person> personDao;
    private Button queryDb;
    private ImageView queryImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        requirePermission();


    }

    private void requirePermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            String[] perms = {"android.permission.WRITE_EXTERNAL_STORAGE"};

            int hasWriteSdcardPermission = ContextCompat.checkSelfPermission(MainActivity.this, perms[0]);

            if (hasWriteSdcardPermission != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(perms, 1000);
            } else {
                initDatabase();
            }

        } else {
            initDatabase();
        }
    }

    private void initDatabase() {
        personDao = BaseDaoFactory.getInstance().getBaseDao(Person.class);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 1000) {
            if (PackageManager.PERMISSION_DENIED == grantResults[0]) {
                Toast.makeText(this, "需要文件访问权限!", Toast.LENGTH_SHORT).show();
            } else {
                initDatabase();
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    private void initView() {
        insertDb = (Button) findViewById(R.id.insert_db);
        insertDb.setOnClickListener(this);
        queryDb = (Button) findViewById(R.id.query_db);
        queryDb.setOnClickListener(this);
        queryImage = (ImageView) findViewById(R.id.query_image);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.insert_db:
                insertDatabase();
                break;
            case R.id.query_db:
                queryDatabase();
                break;
        }
    }

    private void queryDatabase() {
        Person where = new Person();
        List<Person> query = personDao.query(where);
        if(query.size()==0){
            return;
        }
        Person person = query.get(0);
        if(person == null){
            return;
        }
        Bitmap bitmap = BitmapUtils.Byte2Bitmap(person.getIcon());
        queryImage.setImageBitmap(bitmap);


    }

    private void insertDatabase() {
        Person p = new Person();
        p.setUsername("闫文勇");
        p.setPassword("1233211234567");
        p.setAge(32L);
        Bitmap bitmap = getBitmap();
        p.setIcon(BitmapUtils.BitmapConvertToByte(bitmap));
        personDao.insert(p);
    }

    private Bitmap getBitmap() {
        Bitmap bitmap = null;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            Drawable vectorDrawable = getDrawable(R.mipmap.ic_launcher);
            bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
                    vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            vectorDrawable.draw(canvas);
        } else {
            bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        }

        return bitmap;
    }
}
